package my.diplom.web.servlet;

import org.springframework.web.HttpRequestHandler;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;
import java.util.ArrayList;

import my.diplom.dao.service.HostService;
import my.diplom.entity.Host;
import my.diplom.util.JsonUtil;

/**
 * Created by IntelliJ IDEA.
 * User: gb
 * Date: 07.11.2009
 * Time: 3:19:24
 * To change this template use File | Settings | File Templates.
 */
public class JsonServlet  implements HttpRequestHandler {

    private HostService hostService;

    public void setHostService(HostService hostService) {
        this.hostService = hostService;
    }

    @Override
    public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        if (request.getParameter("hostId") != null) {
            Host host = hostService.getById(Long.parseLong(request.getParameter("hostId")), true);
            String json = JsonUtil.resourcesToJson(new ArrayList(host.getResources()));
            response.setContentType("application/json");
            response.getWriter().println(json);
        }
    }
}
